import threading
import random

# 定义全局变量用于存储仿真结果
total_points = 1000000
points_inside_circle = 0

# 定义蒙特卡洛仿真函数
def monte_carlo_simulation(points):
    global points_inside_circle
    for _ in range(points):
        x = random.random()
        y = random.random()
        if x**2 + y**2 <= 1:
            points_inside_circle += 1



def process():
    # 定义线程数
    num_threads = 1

    # 创建线程列表
    threads = []
    # 创建并启动线程
    for _ in range(num_threads):
        thread = threading.Thread(target=monte_carlo_simulation, args=(total_points // num_threads,))
        threads.append(thread)
        thread.start()
    # 等待所有线程执行完毕
    for thread in threads:
        thread.join()
    # 计算圆周率的近似值
    pi_approximation = 4 * points_inside_circle / total_points
    print("Approximation of pi:", pi_approximation)


if __name__ == "__main__":
    import timeit
    t = timeit.timeit(process, number=1)
    print(t)
